﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SharpMap.Presentation.Wpf;
using SharpMap.Layers;
using System.Diagnostics;
using SharpMap.Presentation.Wpf.Converters;
namespace SharpMap.Wpf
{
    //TODO: to be converted to ItemsControl
    public class MapLayer : ContentControl 
    {
        static readonly IValueConverter _strignToLayerConverter = new StringLayerSourceConverter();
        static MapLayer()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MapLayer), new FrameworkPropertyMetadata(typeof(MapLayer)));
        }

        public MapLayer()
        {
        }

        internal void PrapareMapLayer(object item)
        {
            MapView mview = VisualTreeHelper.GetParent(this) as MapView;
            if (mview != null)
            {
                MapViewModel mvm = mview.DataContext as MapViewModel;
                if (mvm != null)
                {
                    //this.DataContext = mvm.GetMapLayerViewModel(this.Content);
  
                    mview.ZoomToExtents();
                }
            }

        }
 
        DrawingLayer _drawingLayer;


        /// <summary>
        /// //TODO: I Know, it should be done using data temlates etc...
        /// For now - it works,
        /// </summary>
        /// <param name="oldContent"></param>
        /// <param name="newContent"></param>
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            if(newContent.GetType() == typeof(string))
            {
                GeometryLayer gl = _strignToLayerConverter.Convert(newContent, typeof(GeometryLayer), null, null) as GeometryLayer;
                Content = gl;
                
            }
            
            GeometryLayer gLayer = newContent as GeometryLayer;
            if (gLayer != null)
            {
                
            }
        }
    }
}
